unit BookGivingStat;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, IBQuery, ExtCtrls;

type
  TfmBookGivingStat = class(TForm)
    cmbOtdel: TComboBox;
    Label1: TLabel;
    btnReport: TButton;
    rgScope: TRadioGroup;
    procedure FormCreate(Sender: TObject);
    procedure btnReportClick(Sender: TObject);
  private
    { Private declarations }
    procedure MakeRep;
    function SelectedOtdel: integer;
  public
    { Public declarations }
  end;

var
  fmBookGivingStat: TfmBookGivingStat;

implementation
uses DataModule, ExcelReport, DicDM;
{$R *.DFM}

procedure TfmBookGivingStat.FormCreate(Sender: TObject);
begin
  if dmData.IsConnected() then begin
    with cmbOtdel do begin
      Items.Assign(dmData.FOtdels);
      ItemIndex := Items.IndexOfObject(pointer(dmData.FOtdel));
    end;
  end;
end;

procedure TfmBookGivingStat.btnReportClick(Sender: TObject);
begin
  MakeRep;
end;

procedure TfmBookGivingStat.MakeRep;
var r: TxlReport;
  q: TIBQuery;
begin
  r := TxlReport.Create;
  q := dmDic.CreateQuery(Self);
  try
    r.Query := q;
    q.SQL.Text := Format('SELECT * FROM GET_DEBTORS(%d)', [SelectedOtdel]);
    if rgScope.ItemIndex = 1 then begin
      q.SQL.Add('WHERE MUST_RETURN_BEFORE < :dt');
      q.ParamByName('dt').AsDate := dmData.FDate;
    end;
    r.Template := RepDir + '\OnHands.xls';
    r.Execute(True);
  finally
    r.Free;
    q.Free;
  end;
end;

function TfmBookGivingStat.SelectedOtdel: integer;
begin
  result := integer(cmbOtdel.Items.Objects[cmbOtdel.ItemIndex]);
end;

end.
